package com.habdqn.servlet.demo;

import com.alibaba.fastjson.JSON;
import com.habdqn.jspdemo.im.enetity.Company;
import com.habdqn.jspdemo.im.service.CompanyService;
import com.habdqn.jspdemo.im.service.impl.CompanyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
@WebServlet(name = "JsonServlet",value = "/json/companies")
public class JsonServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CompanyService companyService = new CompanyServiceImpl();
        List<Company> companies = companyService.findPage(1,20,null);

        //将集合数据转换成JSON格式
        String json = JSON.toJSONString(companies);

        //将JSON格式响应给客户端
        response.setContentType("text/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(json);

    }
}
